package com.mooc.meetingfilm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mooc.meetingfilm.utils.common.vo.LoginParamVO;
import com.mooc.meetingfilm.user.dao.entity.MoocUserT;
import com.mooc.meetingfilm.user.dao.mapper.MoocUserTMapper;
import com.mooc.meetingfilm.user.service.UserService;
import com.mooc.meetingfilm.utils.util.MD5Util;
import com.mooc.meetingfilm.utils.exception.BaseServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private MoocUserTMapper userMapper;
    @Override
    public MoocUserT checkLoginParamVo(LoginParamVO loginParamVO) throws BaseServiceException {
        MoocUserT user = selectByUsername(loginParamVO.getUsername());
        if (Objects.isNull(user)){
            throw new BaseServiceException(404, "用户名不存在");
        }else {
            checkPassword(user.getUserPwd(), loginParamVO.getPassword());
        }
        return user;
    }

    private void checkPassword(String dbPassword, String paramPassword) throws BaseServiceException {
        if (!dbPassword.equals(MD5Util.encrypt(paramPassword))){
            throw new BaseServiceException(404, "密码不正确");
        }
    }

    private MoocUserT selectByUsername(String username) {
        return userMapper.selectOne(new QueryWrapper<MoocUserT>(){{
            eq("user_name", username);
        }});

    }
}
